package com.xiyue.leaspring.po;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.junit.Test;

import com.xiyue.leaspring.util.JPAEntityFactory;

public class JPARelations {

	@Test
	public void testAddCompanyAndDetails(){
		Company company = new Company();//创建持久化类对象
		company.setCname("万里雪花香九霄");//设置属性
		Details details = new Details();//创建持久化类对象
		details.setAddress("清空完杀叫月明");//设置属性
		details.setCapital(50000.0);//设置属性
//		company.setDetails(details);//设置一对一关联
//		details.setCompany(company);//设置一对一关联
		JPAEntityFactory.getEntityManager().getTransaction().begin();//开启事务
		JPAEntityFactory.getEntityManager().persist(company);//持久化数据
		JPAEntityFactory.getEntityManager().getTransaction().commit();//提交事务
		JPAEntityFactory.close();
	}
	
	@Test
	public void testFindCompanyAndDetails(){
		Company company = JPAEntityFactory.getEntityManager().find(Company.class,1L);
		JPAEntityFactory.close();
	}
	
	@Test
	public void testAddCompanyAndDepts(){
		Company company = new Company();//创建持久化类对象 实例话“一”方对象
		company.setCname("含笑太牛云你说就是牛2");//设置属性
		List<Dept> allDepts = new ArrayList<Dept>();//保存所有的部门信息
		for(int i=0;i<10;i++) {
			Dept dept = new Dept();
			dept.setDname("啊撒旦记录卡九点三零客机"+i);
			dept.setCompany(company);
			allDepts.add(dept);
		}
		company.setDepts(allDepts);
		JPAEntityFactory.getEntityManager().getTransaction().begin();//开启事务
		JPAEntityFactory.getEntityManager().persist(company);//持久化数据
		JPAEntityFactory.getEntityManager().getTransaction().commit();//提交事务
		JPAEntityFactory.close();
	}
	
	
	@Test
	public void testFindCompanyAndDepts(){
		Company company = JPAEntityFactory.getEntityManager().find(Company.class,3L);
		company.getDepts().size();
		JPAEntityFactory.close();
	}
	
	@Test
	public void testFindDepts(){
		Dept dept = JPAEntityFactory.getEntityManager().find(Dept.class,3L);
		dept.getCompany().getCname();
		JPAEntityFactory.close();
	}
	
	@Test
	public void testMemberAdd() {
		//member_role关联表里是rid信息，下面定义的都是rid内容
		String rids[] = new String[] {"company","dept","emp"};//定义角色编号
		//设置一堆的Role对象只是为了获取一个rid属性内容，但是关联关系必须要求产生Role集合对象
		List<Role> allRoles = new ArrayList<Role>();//保存角色信息
		Member member = new Member();//实例化持久类对象
		member.setMid("xiyueliushui");//设置数据
		member.setName("天穹圣戟");//设置数据
		for (int i = 0; i < rids.length; i++) {//配置用户角色
			Role role = new Role();//实例化持久类对象
			role.setRid(rids[i]);//设置角色
			allRoles.add(role);//向集合保存角色信息
		}
		member.setRoles(allRoles);//一个用户有多个角色
		JPAEntityFactory.getEntityManager().getTransaction().begin();//开启事务
		JPAEntityFactory.getEntityManager().persist(member);//持久化数据
		JPAEntityFactory.getEntityManager().getTransaction().commit();//提交事务
		JPAEntityFactory.close();
	}
	
	@Test
	public void testFindMember(){
		Member member = JPAEntityFactory.getEntityManager().find(Member.class,"xiyueliushui");
		member.getRoles().size();
		JPAEntityFactory.close();
	}
	

	@Test
	public void testFindMemberSQL(){
		Member member = JPAEntityFactory.getEntityManager().find(Member.class,"xiyueliushui");
		String sql = "select rid from member_role where mid=:mid";//原生SQL
		System.out.println("***用户ID:"+member.getMid()+"、真实姓名:"+member.getName());
		Query query = JPAEntityFactory.getEntityManager().createNativeQuery(sql);
		query.setParameter("mid", member.getMid());
		List<Object> allRids = query.getResultList();
		System.out.println("***用户角色："+allRids);
		JPAEntityFactory.close();
	}
	
	
	
	@Test
	public void testMemberEidt() {
		//member_role关联表里是rid信息，下面定义的都是rid内容
		String rids[] = new String[] {"market","project","sale"};//定义角色编号
		//设置一堆的Role对象只是为了获取一个rid属性内容，但是关联关系必须要求产生Role集合对象
		List<Role> allRoles = new ArrayList<Role>();//保存角色信息
		Member member = new Member();//实例化持久类对象
		member.setMid("xiyueliushui");//设置数据
		member.setName("天穹圣戟1");//设置数据
		for (int i = 0; i < rids.length; i++) {//配置用户角色
			Role role = new Role();//实例化持久类对象
			role.setRid(rids[i]);//设置角色
			allRoles.add(role);//向集合保存角色信息
		}
		member.setRoles(allRoles);//一个用户有多个角色
		JPAEntityFactory.getEntityManager().getTransaction().begin();//开启事务
		JPAEntityFactory.getEntityManager().merge(member);//持久化数据
		JPAEntityFactory.getEntityManager().getTransaction().commit();//提交事务
		JPAEntityFactory.close();//关闭连接
	}
	
}
